//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//mj个人资料

#import "MyProfileController.h"
#import "SetNameController.h"
#import "UpgradeGoutSchemeView.h"
#import "MJPhotoHelper.h"
#import "MJPickerViewAlertView.h"
#import "AllCountryCodeVC.h"

@interface MyProfileController ()
@property (weak, nonatomic) IBOutlet MJUICellImageTextView *nicknameItemView;
@property (weak, nonatomic) IBOutlet MJUICellImageTextView *groupItemView;
@property (weak, nonatomic) IBOutlet MJUICellImageTextView *genderItemView;
@property (weak, nonatomic) IBOutlet MJUICellImageTextView *birthdateItemView;
@property (weak, nonatomic) IBOutlet MJUICellImageTextView *regionItemView;
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (nonatomic, assign) NSInteger genderType;

@end

@implementation MyProfileController


- (void)viewDidLoad {
    self.backgroundColorType = BackgroundColorTypeGray;
    [super viewDidLoad];
   
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)mj_setUI{
    [super mj_setUI];
    
    self.mj_titleLocalizable = @"My Profile";
    
    [self updateUI];
  

}

#pragma mark - 方法
- (void)updateUI {
    UserModel *userModel = UserManager.shared.userModel;
    self.nicknameItemView.subTitle = userModel.userName;
    self.groupItemView.subTitle = userModel.groupName;
    self.genderItemView.subTitleTextField.mj_localizable = userModel.gender;
    self.genderItemView.subTitleTextField.hidden = false;
    self.regionItemView.subTitle = userModel.area ? [HealthHelper getCountryNameWithCountryName:userModel.area] : [HealthHelper getCountryAreaName];
//    self.regionItemView.subTitleTextField.textColor = UIColor.mj_lightGrayColor;
    self.genderItemView.rightImageView.image = MJUIImageMake(@"icon_shangxiajiantou");
    if(userModel.gender){
        self.genderType = [userModel.gender isEqualToString:@"Male"] ? 0:1;
    }else{
        self.genderType = -1;
    }
    
    [self.iconImageView mj_setHeadImageWithURLString:userModel.headPic gender:userModel.gender];
    
    if(userModel.birthday.length){
        self.birthdateItemView.subTitleTextField.text = [userModel.birthday mj_stringConvertToFormatStringByFormat:MJUIDateFormat19 OriginalFormat:MJUIDateFormat2];
    }
    
}

- (IBAction)handleBirthdateButtonClick:(UIButton *)sender {
    
    MJWeakSelf
    MJPickerViewAlertView *selectDateAlertView = [[MJPickerViewAlertView alloc] init];
    selectDateAlertView.date = self.birthdateItemView.subTitleTextField.text;
    selectDateAlertView.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    selectDateAlertView.confirmBlock = ^(NSString *date) {
        weakSelf.birthdateItemView.subTitleTextField.text = date;
        [weakSelf requestData];

    };
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self.navigationController presentViewController:selectDateAlertView animated:NO completion:nil];

}

- (IBAction)handlePictureButtonClick:(UIButton *)sender {
    
    MJWeakSelf
    [MJPhotoHelper.shared getSelectPhotoWithBlock:^(UIImage *image) {
        weakSelf.iconImageView.image = image;
        NSLog(@"mj_calulateImageFileSize=======================%@",[image mj_calulateImageFileSize]);
        UIImage *resizeImage = [image mj_resizeImageTargetSize:CGSizeMake(200, 200)];
        UIImage *newImage = [resizeImage mj_compressToSize:120];
        NSLog(@"Size1111=======================%@",[newImage mj_calulateImageFileSize]);
        [weakSelf uploadPhotoWithImage:newImage];
    }];
}

- (void)uploadPhotoWithImage:(UIImage *)image {
    MJWeakSelf
    [MJHttpNetWorking uploadImageWithParams:@{} requestUri:oss_user_headPic image:image showLoading:true success:^(MJBaseModel *model) {
        
        NSLog(@"uploadPhotoWithImage=======================%@",model.data);
        [MJHttpNetWorking requestWithParams:@{} requestUri:user_update requestType:RequestTypeGet showLoading:false success:^(MJBaseModel *smodel) {
            
            if(smodel.code == 200){
                [UserManager.shared saveUserInfo:smodel.data];
//                [weakSelf updateUI];
            }

        } failure:^(NSError *error) {
            
        }];

    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (IBAction)handleNicknameButtonClick:(UIButton *)sender {
    MJWeakSelf
    SetNameController *vc = [[SetNameController alloc]init];
    vc.titleItemType = TitleItemTypeNickname;
    vc.text = self.nicknameItemView.subTitleTextField.text;
    vc.isMyProfile = true;
    vc.handleTitleBlock = ^(NSString *title) {
        weakSelf.nicknameItemView.subTitleTextField.text = title;
        [weakSelf requestData];

    };
    [self.navigationController pushViewController:vc animated:true];
}

- (IBAction)handleGroupButtonClick:(UIButton *)sender {
    MJWeakSelf
    SetNameController *vc = [[SetNameController alloc]init];
    vc.titleItemType = TitleItemTypeGroupName;
    vc.text = self.groupItemView.subTitleTextField.text;
    vc.isMyProfile = true;
    vc.handleTitleBlock = ^(NSString *title) {
        weakSelf.groupItemView.subTitleTextField.text = title;
        [weakSelf requestData];
    };
    [self.navigationController pushViewController:vc animated:true];
}

- (IBAction)handleGenderButtonClick:(UIButton *)sender {
    MJWeakSelf
    CGRect targetRect = [self.genderItemView.subTitleTextField convertRect:self.genderItemView.subTitleTextField.bounds toView:self.view];
    UpgradeGoutSchemeView *addVC = [[UpgradeGoutSchemeView alloc] init];
    addVC.targetRect = targetRect;
    addVC.type = self.genderType;
    addVC.confirmBlock = ^(NSInteger type) {
        weakSelf.genderType = type;
        if(type == 0){
            weakSelf.genderItemView.subTitleTextField.text = MJUILocalizedString(@"Male");
        }else{
            weakSelf.genderItemView.subTitleTextField.text = MJUILocalizedString(@"Female");
        }
        [weakSelf requestData];
    };
    addVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    self.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self.navigationController presentViewController:addVC animated:NO completion:nil];
}

- (IBAction)handleRegionButtonClick:(UIButton *)sender {
    MJWeakSelf
    AllCountryCodeVC *vc = [[AllCountryCodeVC alloc]init];
    vc.handleCountryCodeBlock = ^(NSDictionary *countryAreaCode) {
        NSString *countryISO = countryAreaCode[@"ISO"];
        weakSelf.regionItemView.subTitle = [HealthHelper getCountryNameWithCountryISO:countryISO];
        [weakSelf requestData];
    };
    [self.navigationController pushViewController:vc animated:true];
}
- (void)requestData{
    
    MJWeakSelf
    NSMutableDictionary *parms = [NSMutableDictionary dictionary];
    if(self.genderType > -1){
        [parms setObject:self.genderType == 0 ? @"Male":@"Female" forKey:@"gender"];
    }
    if(self.groupItemView.subTitleTextField.text.length){
        [parms setObject:self.groupItemView.subTitleTextField.text forKey:@"groupName"];
    }
    if(self.nicknameItemView.subTitleTextField.text.length){
        [parms setObject:self.nicknameItemView.subTitleTextField.text forKey:@"userName"];
    }
    if(self.birthdateItemView.subTitleTextField.text.length){
        [parms setObject:[self.birthdateItemView.subTitleTextField.text stringByAppendingString:@"-01"] forKey:@"birthday"];
    }
    if(self.regionItemView.subTitleTextField.text){
        [parms setObject:self.regionItemView.subTitleTextField.text forKey:@"area"];
    }
    [MJHttpNetWorking requestWithParams:parms requestUri:user_update requestType:RequestTypePut showLoading:true success:^(MJBaseModel *model) {
        
        if(model.code == 200){
            [UserManager.shared saveUserInfo:model.data];
            [weakSelf updateUI];
        }

    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - 代理

#pragma mark - 懒加载

@end
